<template>
	<view class="common-problem">
		<view class="search-box">
			<view class="input-box">
				<view class="search-icon">
					<text class="uni-icon">&#xe466;</text>
				</view>
				<view class="search-input">
					<input type="text" v-model="formData.questionName" placeholder="输入想要搜索的内容" />
				</view>
				<view class="close-icon" v-show="formData.questionName" @click="formData.questionName=''">
					<text class="uni-icon">&#xe460;</text>
				</view>
			</view>
			<view class="search-btn">
				<text @click="getList(true)">搜索</text>
			</view>
		</view>
		<scroll-view class="list-scroll" scroll-y @scrolltolower="getList()">
			<view class="scroll-box">
				<uni-collapse>
					<uni-collapse-item :title="item.questionName" v-for="(item,index) in list" :key="index">
						<view class="content">
							<view class="text" v-html="item.replyNote"></view>
						</view>
					</uni-collapse-item>
				</uni-collapse>
				<empty-waybill v-show="list.length==0&&!loading"></empty-waybill>
				<view class="get-more" v-show="andMore">
					加载中<text class="uni-icon">&#xe465;</text>
				</view>
				<view class="get-more" v-show="list.length&&!andMore">
					<text>没有更多了</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import emptyWaybill from "@/components/tool-empty/waybill";
	export default {
		components: {
			emptyWaybill
		},
		data() {
			return {
				loading: false,
				list: [],
				pages: {
					size: 20, // 每次加载的数量
					current: 1, // 记录当前页
					total: '', // 记录总数
				},
				formData: {
					questionName: ""
				},
				tempData: {}
			}
		},
		computed: {
			andMore() {
				// 计算是否还有更多
				return this.pages.total !== this.list.length;
			}
		},
		methods: {
			// 加载，刷新
			getList(isRefresh) {
				if (isRefresh) {
					Object.assign(this.tempData, this.formData);
					this.pages.current = 1;
					this.list = [];
				} else {
					if (this.andMore) {
						this.pages.current++;
					} else {
						return;
					}
				}
				this.loading = true;

				const formData = {
					...this.tempData,
					...this.pages
				}
				// console.log(formData);
				this.$api.commonProblemPage(formData).then(res => {
					this.loading = false;
					const resData = res.data.data;
					resData.records.forEach(item => {
						if (!item.replyNote) {
							this.$api.commonProblemDetail(item.id).then(detailRes => {
								item.replyNote = detailRes.data.data.replyNote
							})
						}
					})
					this.list.push(...resData.records);
					this.pages.total = resData.total;
				})
			},
			// 模拟请求
		},
		mounted() {
			this.getList(true);
		}
	}
</script>

<style lang="scss">
	@keyframes myMove {
		from {
			transform: rotate(0)
		}

		to {
			transform: rotate(360deg);
		}
	}

	.common-problem {
		background: #f5f5f5;
	}

	.get-more {
		padding: 20rpx;
		text-align: center;
		font-size: 28rpx;

		.uni-icon {
			vertical-align: middle;
			margin-left: 20rpx;
			font-size: 40rpx;
			animation: myMove 2s linear infinite;
		}
	}

	.search-box {
		padding: 30rpx;
		display: flex;
		background-color: #fff;

		.input-box {
			flex: 1;
			display: flex;
			height: 68rpx;
			padding: 0 30rpx;
			background: #F8F8F8;
			border-radius: 58rpx;

			.search-icon {
				display: flex;
				align-items: center;

				.uni-icon {
					font-size: 28rpx;
					color: #666666;
					vertical-align: middle;
				}
			}

			.search-input {
				flex: 1;
				margin: 0 20rpx;

				input {
					font-size: 28rpx;
					height: 100%;
					width: 100%;
				}
			}

			.close-icon {
				display: flex;
				align-items: center;

				.uni-icon {
					font-size: 28rpx;
					color: #fff;
					vertical-align: middle;
					background-color: #D9D9D9;
					border-radius: 50%;
					padding: 4rpx;
				}
			}
		}

		.search-btn {
			background-color: #007AFF;
			font-size: 28rpx;
			line-height: 58rpx;
			border-radius: 60rpx;
			width: 120rpx;
			margin-left: 16rpx;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	.scroll-box {
		padding: 20rpx 30rpx;
	}

	.list-scroll ::v-deep {
		max-height: calc(100vh - 128rpx);
		border-radius: 10rpx;
		overflow: hidden;
	}

	.uni-collapse ::v-deep {

		.uni-collapse-item__title-text {
			color: #333;
			font-size: 28rpx;
		}

		.content {
			padding: 0 30rpx;
			color: #343434;
			font-size: 26rpx;
			line-height: 36rpx;
			padding-bottom: 20rpx;
			text-align: justify;

			img {
				max-width: 100%;
			}
		}
	}
</style>